﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;

namespace produccion.clases
{
    public class CLSMAESEC : ObservableObject
    {
        MDOgeneral mod = new MDOgeneral();
        string codsec_sec, nomsec_sec, repman_sec;//FJRF - Agregamos el nuevo campo

        public string CODSEC_SEC
        {
            get { return codsec_sec; }
            set { codsec_sec = value; RaisePropertyChangedEvent("CODSEC_SEC"); }
        }

        public string NOMSEC_SEC
        {
            get { return nomsec_sec; }
            set { nomsec_sec = value; RaisePropertyChangedEvent("NOMSEC_SEC"); }
        }

        public string REPMAN_SEC//FJRF - Agregamos el campo nuevo
        {
            get { return repman_sec; }
            set { repman_sec = value; RaisePropertyChangedEvent("REPMAN_SEC"); }
        }

        private Boolean _EXISTE = false;
        public Boolean EXISTE
        {
            get { return _EXISTE; }
            set { _EXISTE = value; RaisePropertyChangedEvent("EXISTE"); }
        }

        public void LimpiarValores()
        {
            CODSEC_SEC = "";
            NOMSEC_SEC = "";
            REPMAN_SEC = "";//FJRF - Limpiamos el campo nuevo
            EXISTE = false;
        }

        public Boolean ExisteSeccion()
        {
            try
            {
                string query = "select nomsec_sec from maesec where codsec_sec='" + CODSEC_SEC + "'";
                if (mod.consulta(query).Rows.Count > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable ConsultarTodas()
        {
            try
            {
                string query = "select codsec_sec as Codigo, nomsec_sec, repman_sec as Nombre from maesec order by codsec_sec, nomsec_sec";//FJRF - Se agrega el campo (repman_sec)
                DataTable dt = mod.consulta(query);
                if (dt.Rows.Count.Equals(0))
                {
                    query = "select '' as codsec_sec, '' as nomsec_sec, '' as repman_sec from maesec";//FJRF - Agregamos el campo nuevo (repman_sec)
                    dt = mod.consulta(query);
                    return dt;
                }
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable Consultar()
        {
            try
            {
                string query = "select codsec_sec, nomsec_sec, repman_sec from maesec where codsec_sec='" + CODSEC_SEC + "'";//FJRF - Agregamos el campo nuevo
                DataTable dt = mod.consulta(query);
                if (dt.Rows.Count > 0)
                {
                    CODSEC_SEC = dt.Rows[0][0].ToString();
                    NOMSEC_SEC = dt.Rows[0][1].ToString();
                    REPMAN_SEC = dt.Rows[0][2].ToString();//FJRF - Se agrega este nuevo campo (REPMAN_SEC)
                    EXISTE = true;
                }
                else
                {
                    CODSEC_SEC = "";
                    NOMSEC_SEC = "";
                    REPMAN_SEC = "";//FJRF - Limpiamos la variable
                    EXISTE = false;
                }
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        
        public List<clases.CLSMAESEC> listarSecciones() {

            try{
                string query = "select CODSEC_SEC,TRIM(NOMSEC_SEC) as NOMSEC_SEC, TRIM(REPMAN_SEC) as REPMAN_SEC from maesec order by REPMAN_SEC, CODSEC_SEC, NOMSEC_SEC";//FJRF - Agregamos el campo nuevo y ordemanos por ese mismo campo (REPMAN_SEC)
            DataTable val= mod.consulta(query);

            List<CLSMAESEC> lisSec = new List<CLSMAESEC>();
            foreach (DataRow r in val.Rows) 
            {
                lisSec.Add(new CLSMAESEC
                {
                    CODSEC_SEC = r["CODSEC_SEC"].ToString(),
                    NOMSEC_SEC = r["NOMSEC_SEC"].ToString(),
                    REPMAN_SEC = r["REPMAN_SEC"].ToString()//FJRF - Agregamos el campo nuevo (REPMAN_SEC)
                });
            }
            return lisSec;
            }catch(Exception ex){throw(ex);}
        }

        public int Eliminar()
        {
            try
            {
                string query = "delete from MAESEC where CODSEC_SEC='" + CODSEC_SEC + "'";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Actualizar()
        {
            try
            {
                string query = "update maesec set nomsec_sec='" + NOMSEC_SEC + "', repman_sec = '" + REPMAN_SEC.ToUpper().Trim() + "' where codsec_sec='" + CODSEC_SEC + "'";//FJRF - Agregamos el campo nuevo (REPMAN_SEC)
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Insertar()
        {
            try
            {
                string query = "insert into maesec (codsec_sec, nomsec_sec, repman_sec) values ('" + CODSEC_SEC + "','" + NOMSEC_SEC + "','" + REPMAN_SEC.ToUpper().Trim() + "')";//FJRF - Agregamos el campo nuevo (REPMAN_SEC)
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public DataTable Buscar()
        {
            try
            {
                string query = "select codsec_sec as codigo, nomsec_sec as nombre, repman_sec as manual from maesec where nomsec_sec like '%" + NOMSEC_SEC + "%' order by nomsec_sec asc";//FJRF - Agregamos el campo nuevo (repman_sec)
                return mod.consulta(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //public event PropertyChangedEventHandler PropertyChanged;
    }
}
